package Api;

import Util.HttpUtil;
import Util.MyKey;
import Util.SignEncrypt;
import com.alibaba.fastjson.JSONObject;
import org.bouncycastle.util.encoders.Base64;

import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.text.SimpleDateFormat;
import java.util.Date;



/**
 * 新收款查询Api ，可查周期很长，用的是rsa签名
 */
public class OrderQuery {
    public static void main(String[] args) throws Exception {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date now = new Date();
        String time = sdf.format(now);

        //公共参数
        String method = "heepay.pay.order.query";
        String version ="1.0";
        String merch_id ="1664502";
        String timestamp =time;
        //业务参数

        //汇付宝支付单号，16位H字母开头
        String trade_no ="H2105199435453AW";
        //商户订单号
        String out_trade_no ="20210519111442412";
        //商户订单时间 yyyy-MM-dd HH：mm：ss
        String out_trade_time ="2021-05-19 11:14:42";

        JSONObject biz_content = new JSONObject();
        biz_content.put("out_trade_time",out_trade_time);
        biz_content.put("out_trade_no",out_trade_no);
        biz_content.put("trade_no",trade_no);

        //签名串组装
        String OldSign = "biz_content=" + biz_content
                + "&merch_id=" + merch_id
                +"&method=" + method
                +"&timestamp=" + timestamp
                +"&version=" + version;

        System.out.println("签名串：--------->"+OldSign);
        System.out.println();

        SignEncrypt signEncrypt = new SignEncrypt();

        String privateKeystr = MyKey.privateKeystr1;

        //密钥配置
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        byte[] privateKeyPath = Base64.decode(privateKeystr.getBytes());
        PrivateKey privateKey = keyFactory.generatePrivate(new PKCS8EncodedKeySpec(privateKeyPath));
        //加签
        String sign = signEncrypt.sign(OldSign, privateKey);


        //最终请求数据
        JSONObject param = new JSONObject();
        param.put("method",method);
        param.put("version",version);
        param.put("merch_id",merch_id);
        param.put("timestamp",timestamp);
        param.put("biz_content",biz_content);
        param.put("sign",sign);

        System.out.println("最终请求数据：--------->"+param);
        System.out.println();

        String a = HttpUtil.sendJsonHttpPost("https://pay.heepay.com/API/HywIndex.aspx", String.valueOf(param));

        System.out.println("请求返回参数：--------->"+a);
        System.out.println();

    }
}
